package com.janlei.acc.adapter;

import java.util.HashMap;
import java.util.Map;

/**
 * 适配器模式
 */
public class AdapterStu {
    /**
     * 目的：让无法匹配的接口与类通过一个转接口能够得以使用
     */
}

/**
 * 插座模板
 */
abstract class Socket {
    int voltage;

    int outVoltage() {
        return voltage;
    }
}

/**
 * 中国标座
 */
class Socket4China extends Socket {
    {
        super.voltage = 220;
    }

    @Override
    public int outVoltage() {
        return voltage;
    }

    @Override
    public String toString() {
        return "Socket4China{" +
                "voltage=" + voltage +
                '}';
    }
}

/**
 * 美国标座
 */
class Socket4America extends Socket {
    {
        super.voltage = 110;
    }

    @Override
    public int outVoltage() {
        return voltage;
    }

    @Override
    public String toString() {
        return "Socket4America{" +
                "voltage=" + voltage +
                '}';
    }
}

/**
 * 输出电压接口
 */
interface OutVoltage {
    Socket out();
}

/**
 * 输出电压适配 适配器类
 */
class SocketAdapter extends Socket implements OutVoltage {

    public SocketAdapter(int voltage) {
        super.voltage = voltage;
    }

    private final Map<Integer, Socket> map;

    {
        map = new HashMap(16);
        map.put(220, new Socket4China());
        map.put(110, new Socket4America());
    }

    @Override
    public Socket out() {
        return map.get(voltage);
    }

    @Override
    public String toString() {
        return "SocketAdapter{" +
                "curVoltage=" + voltage +
                ", map=" + map +
                '}';
    }
}